package com.mxc.website.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.json.JSONObject;
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import com.mxc.website.model.Customers;
import com.mxc.website.model.QuestionLibrary;
import com.mxc.website.util.R;
import com.mxc.website.util.SendMessageUtil;
import com.mxc.website.util.WordSegmentationUtil;
import com.mxc.website.websiteService.WebsiteService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.WatchService;
import java.util.*;

/**
 * @author wendongdong
 */
@RestController
@RequestMapping("/website")
@RequiredArgsConstructor
public class WebsiteController {

    private final WebsiteService websiteService;

    @PostMapping("/websiteSubmit")
    public R getWebsite(@RequestBody Customers customers){
        Customers save = websiteService.save(customers);
        MailAccount account = SendMessageUtil.getAccount();

        List<String> emails = new ArrayList<>();
        emails.add("duoyuan.hu@eletile.com");
        String content = "名称：" + save.getCustomerName() + "\n" +
                "客户类型：" + save.getCustomerType() + "\n" +
                "主题：" + save.getTheme() + "\n" +
                "邮箱：" + save.getEmail() + "\n" +
                "消息：" + save.getMessage() + "\n" +
                "电话：" + save.getPhone();
        MailUtil.send(account, CollUtil.newArrayList(emails), "墨西哥官网客户 提醒", content, false);
        return R.ok();
    }

    @GetMapping("/getQuestionLibraryByPosition")
    public R getQuestionLibraryByPosition(String position){
        if (position == null){
            return R.ok();
        }
        List<QuestionLibrary> questionLibrary = websiteService.getQuestionLibrary(position);
        return R.ok(questionLibrary);
    }

    @GetMapping("/getQuestionLibraryByPid")
    public R getQuestionLibraryByPid(String pid){
        List<QuestionLibrary> questionLibrary = websiteService.getQuestionLibraryByPid(pid);
        return R.ok(questionLibrary);
    }

    @GetMapping("/robot")
    public R robot(String text){
        List<String> strings = WordSegmentationUtil.segmentWithoutStopWords(text, "dictionary/dict.txt");
        Map map = new HashMap();
        for (int i = 0; i < strings.size(); i++) {
            List<QuestionLibrary> questionLibraryByText = websiteService.getQuestionLibraryByText(strings.get(i));
            questionLibraryByText.forEach(item->{
                map.put(item,map.get(item) == null ? 1 : (int)map.get(item) + 1);
            });
        }
        // 将 Map 的键值对转换为 List<Map.Entry<Object, Integer>>
        List<Map.Entry<Object, Integer>> entryList = new ArrayList<>(map.entrySet());

        // 使用 Collections.sort 对 List 进行排序
        Collections.sort(entryList, new Comparator<Map.Entry<Object, Integer>>() {
            @Override
            public int compare(Map.Entry<Object, Integer> o1, Map.Entry<Object, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        List r = new ArrayList();
        for (int i = 0; i < entryList.size(); i++) {
            //只要五个
            if (i<=4){
                r.add(entryList.get(i).getKey());
            }
        }
        return R.ok(r);
    }


    public static void main(String[] args) {
        // 待分词的西班牙语文本
        List<String> strings = WordSegmentationUtil.segmentWithoutStopWords("introduccón", "dictionary/dict.txt");
        // 输出分词结果
        for (String term : strings) {
            System.out.println(term);
        }
    }
}